/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package quizzempire.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Observable;
import java.util.Observer;
import quizzempire.model.IModel;
import quizzempire.model.IQuestion;
import quizzempire.model.IQuizz;
import quizzempire.model.statemachine.State;

/**
 *
 * @author Victor
 */
public class GameInterface extends javax.swing.JFrame implements Observer{

    /**
     * Creates new form GameInterface
     */
    IModel model;
    IQuizz iquizz;
    IQuestion iquestion;    
    int questionNumber = 0;
    int score = 0;
    boolean answered=false;
    String[] answers;
    
 
     
    public GameInterface(IModel model) {
        initComponents();
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
        this.model = model;
        iquizz=model.getQuizzViewer();
        iquestion= model.getQuestionViewer();
        // o get do iquestion está dentro do initQuestion()   
        
        lbLogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/quizzempire/resources/gamerlogo.png")));
        
        initFields();
        initQuestion();
        model.addObserver(this);   
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbLogo = new javax.swing.JLabel();
        btNextQuestion = new javax.swing.JButton();
        btMainMenu1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        lbQuizzName = new javax.swing.JLabel();
        lbNumQuestion = new javax.swing.JLabel();
        lbGenre = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        lbDifficulty = new javax.swing.JLabel();
        lbScore = new javax.swing.JLabel();
        label1 = new java.awt.Label();
        label2 = new java.awt.Label();
        jPanel3 = new javax.swing.JPanel();
        label3 = new java.awt.Label();
        lbQuestionNumber = new javax.swing.JLabel();
        lbQuestion = new javax.swing.JLabel();
        btA3 = new javax.swing.JButton();
        btA1 = new javax.swing.JButton();
        btA2 = new javax.swing.JButton();
        btA4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBounds(new java.awt.Rectangle(0, 0, 0, 0));
        setMinimumSize(new java.awt.Dimension(550, 450));
        setPreferredSize(new java.awt.Dimension(600, 450));
        setResizable(false);

        lbLogo.setText("LOGO");

        btNextQuestion.setText("NEXT QUESTION");
        btNextQuestion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNextQuestionActionPerformed(evt);
            }
        });

        btMainMenu1.setText("MAIN MENU");
        btMainMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btMainMenu1ActionPerformed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));

        lbQuizzName.setText("quizz name");

        lbNumQuestion.setText("questions");

        lbGenre.setText("genre");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbQuizzName)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
                .addComponent(lbGenre)
                .addGap(54, 54, 54)
                .addComponent(lbNumQuestion)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lbGenre)
                        .addComponent(lbNumQuestion))
                    .addComponent(lbQuizzName, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(0, 30, Short.MAX_VALUE))
        );

        jPanel2.setBackground(new java.awt.Color(204, 204, 204));

        lbDifficulty.setText("easy");

        lbScore.setText("1");

        label1.setText("Difficulty");

        label2.setText("Score:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbDifficulty)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(lbScore)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(lbDifficulty)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lbScore))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(2, 2, 2)
                .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel3.setBackground(new java.awt.Color(204, 204, 204));

        label3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        label3.setText("Question");

        lbQuestionNumber.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lbQuestionNumber.setText("1");

        lbQuestion.setText("question");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(lbQuestionNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbQuestion, javax.swing.GroupLayout.PREFERRED_SIZE, 346, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbQuestionNumber)
                    .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 34, Short.MAX_VALUE))
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(1, 1, 1)
                .addComponent(lbQuestion, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btA3.setText("A3");
        btA3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btA3ActionPerformed(evt);
            }
        });

        btA1.setText("A1");
        btA1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btA1ActionPerformed(evt);
            }
        });

        btA2.setText("A2");
        btA2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btA2ActionPerformed(evt);
            }
        });

        btA4.setText("A4");
        btA4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btA4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lbLogo, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(44, 44, 44)
                                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(btMainMenu1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btNextQuestion)
                        .addGap(16, 16, 16))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btA3, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btA1, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 70, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btA2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btA4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(lbLogo, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btA1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btA2, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btA3, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btA4, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btNextQuestion)
                    .addComponent(btMainMenu1))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btNextQuestionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNextQuestionActionPerformed
        //TODO - pedir ao modelo para mudar para a proxima pergunta
        model.nextQuestion();      
    }//GEN-LAST:event_btNextQuestionActionPerformed

    private void btMainMenu1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btMainMenu1ActionPerformed
        model.restart();
    }//GEN-LAST:event_btMainMenu1ActionPerformed

    private void btA1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btA1ActionPerformed

        if(!answered){
            if(iquestion.getIndexCorrect()==0) 
            {
                score++;
                btA1.setBackground(Color.green);
            }
            else btA1.setBackground(Color.red);
            answered=true;
            model.setAnswer(0);
        }
    }//GEN-LAST:event_btA1ActionPerformed

    private void btA2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btA2ActionPerformed

        if(!answered){
            if(iquestion.getIndexCorrect()==1) 
            {
                score++;
                btA2.setBackground(Color.green);
            }
            else btA2.setBackground(Color.red);
            answered=true;
            model.setAnswer(1);
        }
    }//GEN-LAST:event_btA2ActionPerformed

    private void btA3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btA3ActionPerformed
        
        if(!answered){
            if(iquestion.getIndexCorrect()==2) 
            {
                score++;
                btA3.setBackground(Color.green);
            }
            else btA3.setBackground(Color.red);
            answered=true;
            model.setAnswer(2);
        }
    }//GEN-LAST:event_btA3ActionPerformed

    private void btA4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btA4ActionPerformed

        if(!answered){
            if(iquestion.getIndexCorrect()==3) 
            {
                score++;
                btA4.setBackground(Color.green);
            }
            else btA4.setBackground(Color.red);
            answered=true;
            model.setAnswer(3);
        }
    }//GEN-LAST:event_btA4ActionPerformed


    //isto de certeza que ta mal
    @Override
    public void update(Observable o , Object arg){
        
        // nao pus answered=false; pk ja esta no initQuestion
       
        State state = model.getState(); 
              
        if (!(state instanceof quizzempire.model.statemachine.AwaitsAnswer))
        {
            model.deleteObserver(this);
            MainBuilder.changeScreen(this, model, state);
        }
        else
            initQuestion();
    }
    
    private void initFields() {
        //comum a todas as perguntas
        //lbLogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/gamerlogo.png")));   
        lbQuizzName.setText("Quizz Name: " + iquizz.getName());
        lbGenre.setText("Genre: " + iquizz.getGenre());    
        lbNumQuestion.setText("Total Questions: " 
                + iquizz.getNumQuestionsLevel()[iquestion.getDifficulty()]);  
        lbScore.setText(""+score); 
        
        String d[] = {"Easy","Medium","Hard"};
        lbDifficulty.setText(d[model.getDifficulty()]);
    }
    
    private void initQuestion(){
        
        String[] diff={"Easy","Normal","Hard"};
        
        iquestion= model.getQuestionViewer(); //TODO in model
        
        //diferente para cada pergunta
        answered=false;
        lbDifficulty.setText(diff[iquestion.getDifficulty()]);
        //TODO  - get from state
        questionNumber++;
        lbQuestionNumber.setText("" + questionNumber);
        lbQuestion.setText(iquestion.getText());
        lbScore.setText(""+score);
        
        answers=iquestion.getAnswers();
        btA1.setText(answers[0]);
        btA2.setText(answers[1]);
        btA3.setText(answers[2]);
        btA4.setText(answers[3]);
        
        btA1.setBackground(Color.white);
        btA2.setBackground(Color.white);
        btA3.setBackground(Color.white);
        btA4.setBackground(Color.white);
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btA1;
    private javax.swing.JButton btA2;
    private javax.swing.JButton btA3;
    private javax.swing.JButton btA4;
    private javax.swing.JButton btMainMenu1;
    private javax.swing.JButton btNextQuestion;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private java.awt.Label label1;
    private java.awt.Label label2;
    private java.awt.Label label3;
    private javax.swing.JLabel lbDifficulty;
    private javax.swing.JLabel lbGenre;
    private javax.swing.JLabel lbLogo;
    private javax.swing.JLabel lbNumQuestion;
    private javax.swing.JLabel lbQuestion;
    private javax.swing.JLabel lbQuestionNumber;
    private javax.swing.JLabel lbQuizzName;
    private javax.swing.JLabel lbScore;
    // End of variables declaration//GEN-END:variables
}
